<template>
  <div class="div">
    <a-row class="top_font">
      <a-col span="5">
        <a-input addon-before="用户昵称: " v-model="search.name"/>
      </a-col>
      <a-col span="5">
        <a-input style="margin-left: 16px" addon-before="用户角色: " v-model="search.role"/>
      </a-col>
      <a-col span="5">
        <a-input style="margin-left: 32px" addon-before="修改用户: " v-model="search.updateBy"/>
      </a-col>
      <a-col span="4">
        <a-button
          style="margin-left: 64px"
          icon="search"
          type="primary"
          htmlType="submit"
          @click="searchList()"
        >查询</a-button>
      </a-col>
    </a-row>
    <a-row class="center_margin">
      <a-col span="5" class="">
        <a-input addon-before="修改时间: " v-model="search.updateTime"/>
      </a-col>
      <a-col span="5">
        <a-input style="margin-left: 16px" addon-before="手机号: " v-model="search.phone"/>
      </a-col>
      <a-col span="5">
        <a-input style="margin-left: 32px" addon-before="邮箱: " v-model="search.email"/>
      </a-col>
      <a-col span="4">
        <a-button
          icon="sync"
          style="margin-left: 64px"
          @click="reset()"
        >重置</a-button>
      </a-col>
    </a-row>
    <a-row class="bottom_font">
      <a-col class="title" span="2">
        用户账号
      </a-col>
      <a-col class="title" span="2">
        用户昵称
      </a-col>
      <a-col class="title" span="2">
        用户角色
      </a-col>
      <a-col class="title" span="2">
        修改用户
      </a-col>
      <a-col class="title" span="2">
        修改时间
      </a-col>
      <a-col class="title" span="2">
        手机号
      </a-col>
      <a-col class="title" span="2">
        邮箱
      </a-col>
      <a-col class="title" span="2">
        社团
      </a-col>
      <a-col class="title" span="2">
        专业
      </a-col>
      <a-col class="title" span="2">
        状态
      </a-col>
      <a-col class="title" span="4">
        操作
      </a-col>
    </a-row>
    <a-list :data-source="data" :pagination="{onChange: (current, size) => {this.num = current, this.searchViews?this.searchInfo():this.getAll()}, showQuickJumper: true, pageSize: 15, total: totalList}">
      <a-list-item slot="renderItem" slot-scope="item, index">
        <a-col class="numb" span="2">
          {{ item.username }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.name }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.role }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.updateBy }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.updateTime }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.phone }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.email }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.club }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.professional }}
        </a-col>
        <a-col class="numb" span="2">
          {{ item.state }}
        </a-col>
        <a-col class="numb" span="4">
          <a-button @click="edit(index)" type="link" size="small" class="operate_change">编辑</a-button>
          <a-button @click="del(index)" type="link" size="small" class="operate_change">删除</a-button>
          <a-button @click="dis(index)" type="link" size="small" class="operate_change">禁用</a-button>
        </a-col>
      </a-list-item>
    </a-list>
    <a-modal
      title="信息修改"
      style="top: 20px;"
      :width="800"
      v-model="editinfo"
      @ok="handleOk()"
    >
      <a-form class="permission-form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="用户账号" hasFeedback>
          <a-input v-model="user.username"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="手机号" hasFeedback>
          <a-input v-model="user.phone"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="邮箱" hasFeedback>
          <a-input v-model="user.email"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="社团" hasFeedback>
          <a-input v-model="user.club"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="专业" hasFeedback>
          <a-input v-model="user.professional"/>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { searchUsers, delUsers, updateUsers, getAllUsers } from '@/api/manage'

export default {
  data () {
    return {
      data: [],
      num: 1,
      totalList: 0,
      searchViews: false,
      editinfo: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      user: { id: '', username: '', phone: '', email: '', club: '', professional: '' },
      search: { pageSize: 15, pageNum: 1, name: '', updateBy: '', updateTime: '', email: '', phone: '', role: '' }
    }
  },
  methods: {
    getAll () {
      const Page = { pageSize: 15, pageNum: this.num }
      getAllUsers(Page).then((res) => {
        this.data = res.datalist
        this.totalList = Number(res.total)
        // console.log(res)
      })
    },
    edit (index) {
      this.$set(this.user, 'username', this.data[index].username)
      this.$set(this.user, 'id', this.data[index].id)
      this.$set(this.user, 'phone', this.data[index].phone)
      this.$set(this.user, 'email', this.data[index].email)
      this.$set(this.user, 'club', this.data[index].club)
      this.$set(this.user, 'professional', this.data[index].professional)
      this.editinfo = true
    },
    dis (index) {
      const User = { id: this.data[index].id, state: '1' }
      updateUsers(User).then((res) => {
        // console.log('success')
      })
    },
    del (index) {
      const Id = { id: this.data[index].id }
      delUsers(Id).then((res) => {
        // console.log('success')
      })
    },
    handleOk () {
      this.editinfo = false
      // console.log(this.user)
      updateUsers(this.user).then((res) => {
        // console.log('success')
      })
    },
    searchList () {
      this.num = 1
      this.searchViews = true
      this.searchInfo()
    },
    searchInfo () {
      this.$set(this.search, 'pageNum', this.num)
      searchUsers(this.search).then((res) => {
        this.data = res.datalist
        this.totalList = Number(res.total)
      })
    },
    reset () {
      this.$set(this.search, 'name', '')
      this.$set(this.search, 'updateBy', '')
      this.$set(this.search, 'updateTime', '')
      this.$set(this.search, 'email', '')
      this.$set(this.search, 'phone', '')
      this.$set(this.search, 'role', '')
      this.num = 1
      if (this.searchViews) {
        this.getAll()
      }
      this.searchViews = false
    }
  },
  created () {
    this.getAll()
  }
}
</script>

<style scoped>
.div{
    padding-right: 20px;
    padding-left: 20px;
    padding-top: 20px;
    padding-bottom: 40px;
    background-color: rgb(255, 255, 255);
    width: 100%;
    height: 100%;
}
.top_font{
    font-size: larger;
}
.center_margin{
    margin-top: 10px;
    font-size: larger;
}
.img{
    height: 20px;
    width: 20px;
    border-radius: 100%;
    margin-left: 48px
}
.bottom_font{
    font-size: larger;
    margin-top: 10px;
    background-color: rgba(241, 247, 247, 0.979);

}
.title{
    text-align: center;
}
.numb{
    text-align: center;
    vertical-align: middle;
    word-break:break-all;
    display:-webkit-box;
    -webkit-line-clamp:1;
    -webkit-box-orient:vertical;
    overflow:hidden;
}
.operate_change{
    color: chartreuse;
}

.operate_delete{
    color: crimson;
     margin-right: 2px;
}

.operate_stop{
    color: darkkhaki;
     margin-right: 2px;
}

.operate_add{
    color:darkturquoise;
    margin-right: 2px;
}
</style>
